//
//  RegisterViewController.m
//  LANDSKY
//
//  Created by 3TI on 14-2-10.
//  Copyright (c) 2014年 qiaquan. All rights reserved.
//

#import "RegisterViewController.h"

#import "TaskWithRegister.h"

@interface RegisterViewController ()<WYHttpTaskDelegate>

@end

@implementation RegisterViewController

@synthesize delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.email.layer.borderColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.4].CGColor;
    self.email.layer.borderWidth = 1.0f;
    
    self.pwd1.layer.borderColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.4].CGColor;
    self.pwd1.layer.borderWidth = 1.0f;
    
    self.pwd2.layer.borderColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.4].CGColor;
    self.pwd2.layer.borderWidth = 1.0f;
    
    self.username.layer.borderColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.4].CGColor;
    self.username.layer.borderWidth = 1.0f;
    
    self.devicenum.layer.borderColor = [[UIColor darkGrayColor] colorWithAlphaComponent:.4].CGColor;
    self.devicenum.layer.borderWidth = 1.0f;
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_board_show:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on_board_hidden:) name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithRegister class]]){
        
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
    MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    mb.labelText = obj.rcvData[@"body"];
    [mb hide:YES afterDelay:.8];
}


#pragma mark Delegate Method

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.email resignFirstResponder];
    [self.pwd2 resignFirstResponder];
    [self.devicenum resignFirstResponder];
    [self.username resignFirstResponder];
    [self.pwd1 resignFirstResponder];
}

#pragma mark Customer Method

-(void)on_board_show:(NSNotification *)n{
    NSDictionary *aDic = n.userInfo;
    DLog(@"%@",aDic);
}
-(void)on_board_hidden:(NSNotification *)n{}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)on_btn_register:(id)sender {
    
    if(!self.username.text.length){
        [UIAlertView showAlertViewWithTitle:@"用户名不能为空"];
        return;
    }
    
    if(!self.pwd1.text.length){
        [UIAlertView showAlertViewWithTitle:@"密码不能为空"];
        return;
    }
    
    if(![self.pwd1.text isEqualToString:self.pwd2.text] && !self.pwd2.text.length){
        [UIAlertView showAlertViewWithTitle:@"用户名不能为空"];
        return;
    }
    
    if(!self.email.text.length){
        [UIAlertView showAlertViewWithTitle:@"邮件不能为空"];
        return;
    }
    
    if(!self.devicenum.text.length){
        [UIAlertView showAlertViewWithTitle:@"昵称不能为空"];
        return;
    }
    
    
    TaskWithRegister *reg = [[TaskWithRegister alloc] init];
    reg.username = self.username.text;
    reg.nickname = self.devicenum.text;
    reg.email = self.email.text;
    reg.pwd = self.pwd1.text;
    reg.delegate = self;
    [reg start];
}
@end
